package org.kongqian.car.service.impl;

import org.kongqian.basic.common.LoginContext;
import org.kongqian.basic.utils.AjaxResult;
import org.kongqian.car.domain.Car;
import org.kongqian.car.domain.CarDetail;
import org.kongqian.car.domain.CarOnlineAuditLog;
import org.kongqian.car.es.CarDoc;
import org.kongqian.car.mapper.CarDetailMapper;
import org.kongqian.car.mapper.CarMapper;
import org.kongqian.car.mapper.CarOnlineAuditLogMapper;
import org.kongqian.car.mapper.CarResourcesMapper;
import org.kongqian.car.service.ICarService;
import org.kongqian.basic.service.impl.BaseServiceImpl;
import org.kongqian.org.domain.Employee;
import org.kongqian.org.service.IBaiduAuditService;
import org.kongqian.org.service.impl.BaiduAuditServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author kq
 * @since 2022-11-03
 */
@Service
public class CarServiceImpl extends BaseServiceImpl<Car> implements ICarService {

    @Autowired
    private CarResourcesMapper carResourcesMapper;

    @Autowired
    private CarDetailMapper carDetailMapper;

    @Autowired
    private IBaiduAuditService baiduAuditService;
    @Autowired
    private CarOnlineAuditLogMapper carOnlineAuditLogMapper;
    @Autowired
    private CarMapper carMapper;

    @Override
    @Transactional
    public void deleteById(Serializable id) {
        super.deleteById(id);

        //删除车辆详情
        carDetailMapper.deleteByCarId(id);

        //删除资源图片
        carResourcesMapper.deleteByCarId(id);
    }

    @Override
    public void insert(Car car) {
        CarOnlineAuditLog log = new CarOnlineAuditLog();
        //车辆的添加修改 自动审核
        String text = car.getTitle()+""+car.getCarDetail().getInfo();
        String inmage = car.getCover();
        AjaxResult ajaxResult = baiduAuditService.baiduAudit(text, inmage, null);
        if(ajaxResult.getSuccess()){
            log.setAuditTime(new Date());
            log.setNote("审核通过！");
            log.setState(1);//1审核通过 2驳回
        }else {
            log.setAuditTime(new Date());
            log.setNote("审核失败！"+ajaxResult.getMessage());
            log.setState(2);
        }

        //保存car的信息
        car.setCreatetime(new Date());//创建时间
        car.setState(0);//状态：0下架 1上架
        car.setAuditstate(log.getState());//状态：0带审核 1审核通过 2驳回

        //设置店铺信息
        Employee loginAdmin = LoginContext.getLoginAdmin();
        car.setShopId(loginAdmin.getShop().getId());
        car.setShopname(loginAdmin.getShop().getName());
        //设置销售人员信息
        car.setSellerId(loginAdmin.getId());
        super.insert(car);

        log.setCarId(car.getId());
        //保存新增日志信息
        carOnlineAuditLogMapper.insert(log);


        //设置车辆详情
        CarDetail carDetail = new CarDetail();
        carDetail.setCarId(car.getId());
        carDetail.setCartitle(car.getTitle());
        carDetail.setInfo(car.getCarDetail().getInfo());
        carDetailMapper.insert(carDetail);

    }

    @Override
    public void onsale(List<Long> ids) {

        if(ids == null || ids.size()<1){//校验参数
            throw new RuntimeException("参数错误");
        }
        int countByids = carMapper.getCountByids(ids);//通过ids查询审核通过的个数
        if(ids.size() != countByids){
            throw new RuntimeException("所选内容有未通过审核的");
        }
        //修改状态 和 上架时间
        carMapper.onsale(ids);

        //存入es


        //添加上架日志

    }

    @Override
    public void offsale(List<Long> ids) {
        if(ids == null || ids.size()<1){//校验参数
            throw new RuntimeException("参数错误");
        }
        //修改状态 和 上架时间
        carMapper.offsale(ids);
    }
}
